package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsChgBean;
import com.koron.css2.meterService.bean.MsChgPlanBean;
import com.koron.css2.meterService.bean.MsChgPlanDetailBean;
import com.koron.css2.meterService.mapper.MsChgMapper;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 快捷换表删除
 * @author admin
 * @Date 2022.02.28
 *
 */
public class MsChgDeleteNew implements ServerInterface {
	
	private static Logger log = LogManager.getLogger(MsChgDeleteNew.class);

	/**
	 * 1、删除单据总表
	 * 2、删除单据明细表
	 * 3、修改用户表的水表编号为旧表
	 * 4、修改用户表的最后抄码为旧表上期抄码
	 * 5、将旧表的状态置为已用
	 * 6、将新表的状态置为在库
	 * 7、删除水表变更记录
	 * 8、删除水表操作表
	 * 9、删除换表记录
	 */
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", Map.class);
		try {    
			Map<String, String> map = JsonUtils.objectToPojo(req.getData(), Map.class);
			if(StringUtils.isBlank(map.get("id"))) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "id不能为空", Void.class);
			}
			String id = map.get("id");
			MsChgPlanDetailMapper chgDetailMapper = factory.getMapper(MsChgPlanDetailMapper.class);
			MsChgPlanMapper chgPlanMapper = factory.getMapper(MsChgPlanMapper.class);
			MsChgMapper chgMapper = factory.getMapper(MsChgMapper.class);
			MsChgPlanDetailBean detail = chgDetailMapper.selectById(id);
			if(detail == null) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "换表详情信息不存在", Void.class);
			}
			MsChgBean msChg = chgMapper.selectOne(detail.getUserNo(), detail.getOldMeterNo(), detail.getNewMeterNo());
			if(msChg.getUseState() == 1) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "已进行抄表，不能对该换表信息进行删除", Void.class);
			}
			MsChgPlanBean msChgPlan = chgPlanMapper.selectMsChgPlan(detail.getReceiptId());
			UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
			UserBean user = userInfoMapper.getUserInfoByUserNo(detail.getUserNo(), UserBean.USER);
			user.setMeterType(detail.getOldMeterType());
			user.setChangeMeterDate(null);
			user.setUpdateInfo(userInfo);
			user.setMeterNo(detail.getOldMeterNo());
			user.setLastReadingNum(detail.getUpperNum());		//暂不修改 最后抄码
			int iCount = userInfoMapper.userInfoUpdate(user, UserBean.USER);
			if(iCount > 0) {
				log.info("换表信息修改成功 用户号：{} 水表编号：{}", detail.getUserNo(), detail.getOldMeterNo());
			}
			MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
			iCount = msInfoMapper.updateMsInfoStatus(detail.getOldMeterNo(), 2);
			if(iCount > 0) {
				log.info("水表编号：{} 已修改为在用", detail.getOldMeterNo());
			}
			iCount = msInfoMapper.updateMsInfoStatus(detail.getNewMeterNo(), 1);
			if(iCount > 0) {
				log.info("水表编号：{} 已修改为在库", detail.getNewMeterNo());
			}
			ChangeUserMapper changeUserMapper = factory.getMapper(ChangeUserMapper.class);
			iCount = changeUserMapper.deleteByReceiptId(msChgPlan.getId());
			if(iCount > 0) {
				log.info("已删除水表用户变更记录");
			}
			iCount = chgDetailMapper.deleteById(detail.getId());
			if(iCount > 0) {
				log.info("已删除换表明细");
			}
			iCount = chgPlanMapper.deleteById(detail.getReceiptId());
			if(iCount > 0) {
				log.info("已删除换表单据");
			}
			iCount = chgMapper.delete(detail.getUserNo(), detail.getOldMeterNo(), detail.getNewMeterNo());
			if(iCount > 0) {
				log.info("已删除换表记录");
			}
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("换表删除失败");
			log.error("换表删除失败", e);
		}
		return info;
	}

}
